#
# @lc app=leetcode.cn id=202 lang=python3
#
# [202] 快乐数
#

# @lc code=start
class Solution:
    def isHappy(self, n: int) -> bool:
        seen = set()
        while n != 1 and n not in seen:
            seen.add(n)
            n = self.nextNum(n)
        return n == 1


    def nextNum(self, lastNum):    
        sum = 0
        while lastNum > 0:
            r = lastNum % 10
            sum += r ** 2
            lastNum //= 10
        return sum


# @lc code=end

